FROM  ekidd/rust-musl-builder:1.48.0 as cargo-build
USER root
RUN rustup default stable
RUN rustup target add x86_64-unknown-linux-musl

RUN sed -i "s@http://\(deb\|security\).debian.org@https://mirrors.aliyun.com@g" /etc/apt/sources.list
RUN  apt-get clean
RUN  apt-get update
RUN apt-get install -y protobuf-compiler
RUN apt-get install -y musl musl-dev musl-tools

RUN mkdir -p /root/.cargo
COPY docker/.cargo/config /root/.cargo/
RUN export CARGO_HTTP_DEBUG=true
RUN export CARGO_LOG=cargo::ops::registry=debug

RUN mkdir -p /home/axum-grpc-demo
RUN mkdir -p /home/axum-grpc-demo/.cargo
COPY docker/.cargo/config /home/axum-grpc-demo/.cargo/
WORKDIR /home/axum-grpc-demo
COPY Cargo.toml Cargo.toml
RUN mkdir src/
RUN echo "fn main() {println!(\"if you see this, the build broke\")}" > src/main.rs
RUN RUSTFLAGS=-Clinker=musl-gcc cargo build -v --release --target=x86_64-unknown-linux-musl
RUN rm -f target/x86_64-unknown-linux-musl/release/deps/axum-grpc-demo*
COPY proto /home/axum-grpc-demo/proto
COPY src /home/axum-grpc-demo/src
COPY build.rs /home/axum-grpc-demo/
RUN RUSTFLAGS=-Clinker=musl-gcc cargo build -v --release --target=x86_64-unknown-linux-musl


FROM alpine:latest
USER root
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories


RUN mkdir -p /home/myapp/bin/
WORKDIR /home/myapp/bin/

COPY --from=cargo-build /home/axum-grpc-demo/target/x86_64-unknown-linux-musl/release/axum-grpc-demo .

RUN addgroup -g 1000 myapp
RUN adduser -D -s /bin/sh -u 1000 -G myapp myapp
RUN chown myapp:myapp axum-grpc-demo
RUN chgrp -R myapp /tmp
RUN chmod -R g+w /tmp
#时区设置上海
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone
USER myapp
EXPOSE 8080
ENTRYPOINT   ./axum-grpc-demo